package aole.visq.draw;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.HashMap;

public class DrawAnimation implements Serializable {
	private static final long					serialVersionUID	= 1L;

	// image
	private int									width, height;
	private Color								bgcolor;
	private HashMap< Integer, SerializedBI >	keys;
	// animation
	private int									totalFrames;
	private int									currentFrame;
	private int									fps;

	public DrawAnimation( int w, int h, int frames ) {
		width = w;
		height = h;
		totalFrames = frames;
		currentFrame = 1;
		fps = 15;
		keys = new HashMap<>();
		bgcolor = Color.white;
		createKey();
	}

	public void setFPS( int fps ) {
		this.fps = fps;
	}

	public int getFPS() {
		return fps;
	}

	public void createKey() {
		if( keys.get( currentFrame ) != null ) return;

		SerializedBI image = new SerializedBI( width, height,
				BufferedImage.TYPE_INT_ARGB );
		keys.put( currentFrame, image );
	}

	public SerializedBI removeKey() {
		return keys.remove( currentFrame );
	}

	public int getCurrentFrame() {
		return currentFrame;
	}

	public void setCurrentFrame( int frame ) {
		currentFrame = frame;
	}

	public int getTotalFrames() {
		return totalFrames;
	}

	public void setTotalFrames( int frames ) {
		totalFrames = frames;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public Color getBackgroudColor() {
		return bgcolor;
	}

	public void setBackgroudColor( Color c ) {
		bgcolor = c;
	}

	public BufferedImage getFinalImage() {
		BufferedImage img = new BufferedImage( getWidth(), getHeight(),
				BufferedImage.TYPE_INT_ARGB );
		Graphics2D g = img.createGraphics();
		g.setColor( getBackgroudColor() );
		g.setBackground( getBackgroudColor() );
		g.fillRect( 0, 0, getWidth(), getHeight() );
		g.drawImage( getImage(), 0, 0, null );
		
		return img;
	}

	public BufferedImage getImage() {
		SerializedBI img;

		for( int i = currentFrame; i > 0; i-- ) {
			img = keys.get( i );
			if( img != null ) return img.getImage();
		}
		return null;
	}

	public void setImage( Image img ) {
		Graphics2D g = getGraphics();
		if( g != null ) g.drawImage( img, 0, 0, null );
	}

	public BufferedImage getPreviousImage() {
		SerializedBI img;

		for( int i = currentFrame - 1; i > 0; i-- ) {
			img = keys.get( i );
			if( img != null ) return img.getImage();
		}
		return null;
	}

	public BufferedImage getNextImage() {
		SerializedBI img;

		for( int i = currentFrame + 1; i <= totalFrames; i++ ) {
			img = keys.get( i );
			if( img != null ) return img.getImage();
		}
		return null;
	}

	public Graphics2D getGraphics() {
		SerializedBI bi = keys.get( currentFrame );
		if( bi == null ) return null;
		return bi.getGraphics();
	}

	public HashMap< Integer, SerializedBI > getKeys() {
		return keys;
	}

	public void drawImage( BufferedImage img ) {
		SerializedBI bi = keys.get( currentFrame );
		if( bi == null ) return;
		bi.drawImage( img );
	}
}
